package com.xfunc.views.viewflipper;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.xfunc.views.R;

public class AppViewFlipper extends ViewFlipper implements View.OnTouchListener, View.OnClickListener {
    public AppViewFlipper(Context context) {
        super(context);
        init();
    }

    public AppViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private GestureDetector mGestureDetector;
    private OnPagerChange onPagerChange;

    public void setOnPagerChange(OnPagerChange onPagerChange) {
        this.onPagerChange = onPagerChange;
    }

    private void init() {
        mGestureDetector = new GestureDetector(new SimpleGestureListener());
        setOnTouchListener(this);
    }

    @Override
    public void onClick(View view) {

    }

    public static final int FLING_MIN_DISTANCE = 80;
    public static final int FLING_MIN_VELOCITY = 120;
    private boolean isFlag = false;

    private void showNextAnimation() {
        int childCount = getChildCount();
        Log.d("viewflipper2","右----》左侧滑动，下一页  position:" + position + "  childCount:"+childCount);
        if (position == 1) return ;
        position = position + 1;
        clearAnimation();
        setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_right_in));
        setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_left_out));
        showNext();
        if(onPagerChange!=null){
            onPagerChange.onChange(position);
        }
    }
    private void showPreviousAnimation() {
        int childCount = getChildCount();
        Log.d("viewflipper2","左---》右侧滑动，上一页 position:" + position + "  childCount:"+childCount);
        if (position == 0) return ;
        position = position - 1;
        clearAnimation();
        setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_left_in));
        setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_right_out));
        showPrevious();
        if(onPagerChange!=null){
            onPagerChange.onChange(position);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        super.onTouchEvent(event);
        return mGestureDetector.onTouchEvent(event);
    }

    public boolean onTouchEvent(MotionEvent event) {
        // 执行touch 事件
        super.onTouchEvent(event);
        return this.mGestureDetector.onTouchEvent(event);
    }

    //这个方法会先执行，当返回为true时，才执行onTouchEvent 方法
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //先执行滑屏事件
        mGestureDetector.onTouchEvent(ev);
        super.dispatchTouchEvent(ev);
        return true;
    }

    int position = 0;

    private class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            setClickable(true);
            return super.onDown(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            setClickable(false);
            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                showNextAnimation();
                isFlag = true;
            } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                showPreviousAnimation();
                isFlag = true;
            } else {
                isFlag = false;
            }
            return false;
        }
    }
    interface OnPagerChange{
        void onChange(int position);
    }
}
